<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use think\facade\Session;
use think\response\Json;
use app\model\CatModel;
use app\controller\Login;
use app\Common;

class Index extends BaseController
{
    //常用数据表内容 及常用字典
    var $cats=[];
    var $dept=[];
    var $users =[];
    var $status=array('1'=>'空闲','2'=>'使用中','3'=>'停用');


    public function __construct(){

        //准备一个类型数组
        $cm=new CatModel();
        $this->cats=$cm->catTree();

        $list = Db::table("dept")->select()->toArray();
        foreach($list as $k=>$v){
            $this->dept[$v['id']]=$v;
        }     
        $list = Db::table("user")->select()->toArray();
        foreach($list as $k=>$v){
            $this->users[$v['id']]=$v;
        }


    }

    //首页
    public function index(){

        $all = Request::param(); 
        $uid = Session::get("logonid");

        if(empty($all['token']) || empty($uid)){
            //return header("location:/index.php/login/index");
        }

        

        // $login =new Login();
        // $arr = $login->check_token($all['token']); 
        // //登录失败
        // if($arr['code']<1){
        //     return json($arr);
        // }

        $sys = Db::table("sys")->find(1);
        $hots = Db::table('goods')->where('hot',1)->limit(30)->column('id,name,price,price1,sales,image');
        foreach($hots as &$row){
            $row['image'] = explode(",",$row['image'])[0];
        }

        $shops = Db::table('dept')->where('status',1)->where('type',2)->limit(4)->column('id,name');
        foreach($shops as &$row){
            $row['shop_img'] = Db::table('dept_info')->where('dept_id',$row['id'])->column('shop_img');
        }
        $sys['top_images']=  explode(",",$sys['top_images']);
        
        unset($sys['agreement']);   
        //halt($sys);     
        View::assign(['sys'=>$sys,'cat'=>$this->cats,'hots'=>$hots,'shops'=>$shops]);
        
        return View::fetch('index');

    }

    //会员管理
    public function customer(){
        $list = Db::table("customer")->where('status',1)->select()->toArray();
        return json($list);
    }  

    //购物车
    public function map(){
        
        return View::fetch();
    }

    //最新资讯
    public function news(){
        $all = Request::param(); 
        $uid = Session::get("logonid");


        $list = Db::table("news")->where('status',1)->order('id','desc')->paginate(30)->toArray();
        foreach($list['data'] as &$kv){
             $kv['content2'] =strip_tags(htmlspecialchars_decode($kv['content']));
            }
        if(!empty($all['app'])){
            return json(['code'=>1,'data'=>$list]);
        }else{
            View::assign(['list'=>$list]);
            return View::fetch('admin/news');
        }
    }   


    public function news_detail(){
        $all= Request::param();

        if(empty($all['id'])) return json(['code'=>0,'msg'=>'news id?']);

        $row = Db::table('news')->find($all['id']);
        return json(['code'=>1,'data'=>$row]);

    }

    //资讯新增与修改
    public function news_edit(){
        $all = Request::param(); 

        //var_dump($all);
        //资讯编辑
        if(!empty($all['id'])){
            //提交修改
            if(!empty($all['post_edit'])){
                if(empty($all['title']) || empty($all['content'])){
                    return json(['code'=>0,'msg'=>'无效的资讯内容!']);
                }

                if($_FILES['file']['size']>0){
                    $files =Common::upload();
                    if($files['code']){
                        $all['image']=$files['msg'];
                    }else{
                        return ($files['msg']);
                    }
                }

                unset($all['post_edit']);
                $res =Db::table('news')->save($all);

                $list = Db::table("news")->order('id','desc')->paginate(30)->toArray();   
                foreach($list['data'] as &$kv){
                    $kv['content2'] =strip_tags(htmlspecialchars_decode($kv['content']));
                }                     
                View::assign(['list'=>$list]);
                return View::fetch('admin/news');
            }else{
                //进入修改
                $row = Db::table('news')->find($all['id']);
                View::assign(['data'=>$row]);
                return View::fetch('admin/news_edit');
            }


        }else{
            //新增资讯
            if(empty($all['title']) || empty($all['content'])){

                return View::fetch('admin/news_add');
            }
            $all['status']=1;
            $all['create_time']=date('Y-m-d H:i:s');
            $all['creater'] = Session::get('loginid');
            $res =Db::table('news')->save($all);

            $list = Db::table("news")->order('id','desc')->paginate(30)->toArray();   
            foreach($list['data'] as &$kv){
                $kv['content2'] =strip_tags(htmlspecialchars_decode($kv['content']));
            }                     
            View::assign(['list'=>$list]);
            return View::fetch('admin/news');
        }
    } 


    //菜单管理
    public function menu(){
        $list = Db::table("menu")->where('status',1)->select();
        return json($list);
    }

    //用户管理
    public function user(){
        $list = Db::table("user")->where('status',1)->select();
        return json($list);
    }    

    //我的资料
    public function me(){
        $uid = Session::get('uid');
        $list = Db::table("user")->where('uid',$uid)->select();
        return json($list);
    } 

    public function api_list(){
        $all=  Request::param();
        $list = Db::table('api_list')->where('status',1)->select()->toArray();
        View::assign(['list'=>$list]);
        return View::fetch();

    }

    //首页信息　
    public function top_images(){
        $all=Request::param();  
        if(empty($all['app'])){
            return json(['code'=>0,'msg'=>':(']);
        }           

        $list = Db::table("sys")->find(1);
        $list['top_images'] =  explode(",",$list['top_images']);

        // $sql="select customer_id,money,create_time,type from acc_log where type in(4,7) and abs(money)>0.1 and create_time>? order by create_time limit 50";
        // $money_1=Db::query($sql,[date('Y-m-d')]);

        // $str ='';
        // foreach($money_1 as &$row){
        //     $cname = Db::table('customer')->where('id',$row['customer_id'])->value('name');
        //     $str.= "恭喜".trim($cname).'同学'. ($row['type']==4 ? "抢到红包":"提现")."【".abs($row['money'])."】！ ";
        // }
        // $list['msg']=$str>'1000'?$str:$list['msg'];

        $list['msg']='';
        $list['use_address']=Db::table('address')->where('customer_id',1)->where('status',1)->select()->toArray();
        $list['use_title']='名山古刹';

        unset($list['agreement']);

        return json($list);
        
    }    

    //学车须知
    public function xcxz(){
        $all=Request::param();

        if(!empty($all['id'])){
            unset($all['post_edit']);
            Db::table('knowledge')->save($all);
        }

        $row = Db::table('knowledge')->find(1);
        if(!$row) $row=['id'=>'','title'=>'学车须知','text'=>'内容编辑'];

        if(!empty($all['app'])){
            return json(['code'=>1,'data'=>$row]);
        }

        View::assign(['data'=>$row]);
        return View::fetch('admin/xcxz');


    }  

    public function agreement(){
        
        $uid = Session::get('logonid'); 
        // if(empty($uid)) return header("location:/login/index/target/admin");              

        $all=Request::param();
        if(!empty($all['id'])){
            $res = Db::table('sys')->update($all);
        }

        $list = Db::table("sys")->where('id',1)->column('id,sys_name,agreement');

        View::assign(['data'=>$list[0]]);
        if(!empty($uid))  return View::fetch('/admin/agreement');

        return json(['code'=>1,'data'=>$list[0]]);
    }

    //system infomation
    public function sys(){
        $uid = Session::get('logonid');
        $all=Request::param();

        //halt($all);
        if(!empty($all['sys_name'])){
            $all['top_images']=[];

            //开屏图
            if($_FILES['pop_img']['size']>0){
                $files =Common::upload($_FILES['pop_img']);
                if($files['code']){
                    $all['pop_img']=$files['msg'];
                }
                unset($_FILES['pop_img']);
            }

            $res = Db::table('sys')->update($all);
        }

        $list = Db::table("sys")->find(1);
        $list['top_images'] = explode(",",$list['top_images']);
        foreach($list['top_images'] as &$img){
            $img= "http://".$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT'].$img;
        }

        if(!empty($all['app'])) {
            unset($list['agreement']);
            return json($list);
        }

        if(empty($uid)) return header("location:/login/index/target/admin");

        View::assign(['sys'=>$list]);
        return View::fetch('sys');
    }

   
}
